package com.kwz.common.ui;

import java.awt.Component;

import javax.swing.JScrollPane;

import com.kwz.common.IComparable;
import com.kwz.common.IContext;
import com.kwz.common.designpatterns.factory.BadContextException;
import com.kwz.common.ui.events.IUiEventDefinition;

/**
 * <p>
 * 			Implementation d'un JScrollPane
 * </p> 
 *
 * @author Ccr
 */
public class UiJScrollPane extends UiJComponent implements IUiJScrollPane {

    // Statiques
    public static final IUiEventDefinition[] EVENTS_DEFS = new IUiEventDefinition[]{
            UI.CHAIN(UiJComponent.EVENTS_DEFS)
    };
    
    /**
     * @throws BadContextException
     */
    public UiJScrollPane() throws BadContextException {
        this((IContext)null);
    }

    /**
     * @param component
     * @throws BadContextException
     */
    public UiJScrollPane(JScrollPane component) throws BadContextException {
        this(createContextFromComponent(null, component));
    }

    /**
     * @param id
     * @param container
     * @throws BadContextException
     */
    public UiJScrollPane(IComparable id, JScrollPane container) throws BadContextException {
        this(createContextFromComponent(id, container));
    }

    /**
     * @param context
     * @throws BadContextException
     */
    public UiJScrollPane(IContext context) throws BadContextException {
        super(context);
    }

    // ----------------------------------- IUiComponent ---------------------------------
    /* (non-Javadoc)
     * @see com.kwz.common.ui.IUiComponent#getEventDefinitions()
     */
    public IUiEventDefinition[] getEventDefinitions() {
        return EVENTS_DEFS;
    }

    // ------------------------------------ IUiJScrollPane ----------------------
    /* (non-Javadoc)
     * @see com.kwz.common.ui.IUiJScrollPane#getJScrollPane()
     */
    public JScrollPane getJScrollPane() {
        return (JScrollPane)getComponent();
    }
    
    /* (non-Javadoc)
     * @see com.kwz.common.ui.IUiJScrollPane#setViewportView(com.kwz.common.ui.IUiComponent)
     */
    public void setViewportView(IUiComponent component) throws UiException {
        Component old = getJScrollPane().getViewport().getView();
        if (old!=null){
            IUiComponent uiOld = UI.getUiComponentFromComponent(old);
            if (uiOld!=null){
                unregisterSubComponent(uiOld.getId());
            }
        }
        registerSubComponent(component);
        getJScrollPane().setViewportView(component.getComponent());
    }
}
